class PostsReq {
    //var url = serviceEndPoint + user +"/?tree=true&username=" + user;
  Blog _blog;  
  String _url;
    
  PostsReq(Blog blog, String url){
    _blog = blog;
    _url = url;
  }

    XMLHttpRequest getPostsFromServer(String method, String params, String toSend, Queue page) {
      XMLHttpRequest request = new XMLHttpRequest();
      request.on.readyStateChange.add((Event event) {
        if (request.readyState != 4) return;
          if (request.status == 200) {
            Map json = JSON.parse(request.responseText); 

            for(var i = 0; i< json.length; i++){
              
              Map newPost = new Map();
              newPost['id'] = json[i]['id'];
              newPost['user'] = json[i]['user'];
              newPost['pubDate'] = json[i]['pubDate'];
              newPost['title'] = json[i]['title'];
              newPost['content'] = json[i]['content'];
              newPost['tags'] = json[i]['tags'];
              
              page.add(newPost);
            }
			
			// update veiw...
			
			_blog.updateButtons();
        			
          } else {  //on error
            document.query('#posts').innerHTML = 'error )-: while receiving posts';
          }
      });
      request.open(method, _url+params, true);
	  request.send(toSend);
      return request;

    }
  
	void loadNextPage(Queue nextPage){
	  String lastID = _blog.getLastID();
      if(lastID == null){
		window.alert("ERROR in loadNextPage: lastID == null");
     	  return;
	  }
		String params = "?getallposts=true&id=" + lastID;
		getPostsFromServer("GET", params, null, nextPage);
	}
	
	void loadPrevPage(Queue prevPage){
	  String firstID = _blog.getFirstID();
      if(firstID == null){
		window.alert("ERROR in loadPrevPage: firstID == null");
     	  return;
	  }
 
      String params = "?getallposts=true&prev=true&id=" + firstID;
	  
	
	  getPostsFromServer("GET", params, null, prevPage);
	}
	
	void loadCurrPage(Queue currPage){
		getPostsFromServer("GET", "?getallposts=true", null, currPage);
	}   
}
